# _*_coding:utf-8_*_
from ATS.instrumentDriver import InstGen
import time


class SMU_B2901BL(InstGen.SMU):
    def __init__(self, name, channel=1):
        InstGen.SMU.__init__(self, name)
        self.channel = channel

    def set_sweep_setting(self, start_v, stop_v, point_v, compliance=0.001):
        self.handler.write("*RST")
        self.handler.write(":FORMat:DATA ASCii")
        self.handler.write(":OUTPut{}:LOW FLOat".format(self.channel))
        self.handler.write(":SOURce{}:FUNCtion:MODE VOLTage".format(self.channel))
        self.handler.write(":SENSe{}:VOLTage:DC:RANGe:AUTO:LLIMit 1".format(self.channel))
        self.handler.write(":SOURce{}:VOLT:MODE SWE".format(self.channel))
        self.handler.write(":SENS{}:CURR:NPLC 1".format(self.channel))
        self.handler.write(":SOURce{}:VOLT:STAR {}".format(self.channel, start_v))
        self.handler.write(":SOURce{}:VOLT:STOP {}".format(self.channel, stop_v))
        self.handler.write(":SOURce{}:VOLT:POIN {}".format(self.channel, point_v))
        self.handler.write(":SOURce{}:SWEep:POIN {}".format(self.channel, point_v))
        self.handler.write(":SENS{}:CURR:RANG:AUTO:LLIM 1.00E-08".format(self.channel))
        self.handler.write(":SENS{}:CURR:PROT:LEVel {}".format(self.channel, compliance))
        self.handler.write("TRIG{}:ALL:COUNt {}".format(self.channel, point_v))
        self.handler.write(":OUTP{}:SAVE 0".format(self.channel))

    def set_voltage(self, voltage, channel):
        self.handler.write(":SOURce{}:VOLT {}".format(channel, voltage))
        self.handler.write(":OUTP{}:STATe 1".format(channel))  # 打开输出
        self.handler.write(":INIT (@{})".format(channel))

    def output_close(self, channel):
        self.handler.write(":OUTP{}:STATe 0".format(channel))  # 关闭输出

    def set_spot_setting(self, voltage, compliance, channel):
        # FIX,
        self.handler.write("*RST")
        self.handler.write(":FORMat:DATA ASCii")
        self.handler.write(":OUTPut{}:LOW GRO".format(channel))
        # self.handler.write(":OUTPut{}:LOW FLOat".format(channel))
        self.handler.write(":SOURce{}:FUNCtion:MODE VOLTage".format(channel))
        self.handler.write(":SOURce{}:VOLT {}".format(channel, voltage))
        self.handler.write(":SENSe{}:VOLTage:DC:RANGe:AUTO:LLIMit 1".format(channel))
        self.handler.write(":SENS{}:CURR:NPLC 1".format(channel))
        self.handler.write(":SOURce{}:VOLT:MODE FIX".format(channel))
        self.handler.write(":SENS{}:CURR:RANG:AUTO:LLIM 1.00E-08".format(channel))
        self.handler.write(":SENS{}:CURR:PROT:LEVel {}".format(channel, compliance))
        self.handler.write(":OUTP{}:STATe 1".format(channel))  # 打开输出
        self.handler.write(":INIT (@{})".format(channel))  # 上电

    def trace_point(self):
        return self.handler.ask(":TRACe:POINts?")

    def trace_free(self):
        return self.handler.ask(":TRACe:FREE?")

    def set_biasv_init(self):
        self.handler.write("*RST")
        self.handler.write(":OUTP{}:SAVE 1".format(self.channel))

    def get_current(self,channel=1):
        current = self.handler.ask(":FETCh:CURRent? (@{})".format(channel)).strip()
        return current

    def measure(self, channel=1):
        try:
            self.handler.write(":OUTP{}:STATe 1".format(channel))  # 打开输出
            self.handler.write(":INIT (@{})".format(channel))  # 上电
            y_result = self.handler.ask(":FETCh:CURRent? (@{})".format(channel)).strip()
            x_result = self.handler.ask(":FETCh:VOLT? (@{})".format(channel)).strip()
        except Exception as e:
            self.handler.write(":OUTPut{}:STATe 0".format(channel))
        else:
            # 关闭
            self.handler.write(":OUTPut{}:STATe 0".format(channel))
            return x_result, y_result


    def trigger_measurement(self):
        return self.handler.ask(":MEASure?")

    def measure_array(self):
        try:
            self.handler.write(":OUTP{}:RECall 0".format(self.channel))  # iv 测试
            self.handler.write(":OUTP{}:STATe 1".format(self.channel))  # 打开输出
            self.handler.write(":INIT (@{})".format(self.channel))  # 上电
            y_result = self.handler.ask(":FETCh:ARRay:CURRent? (@{})".format(self.channel))
            x_result = self.handler.ask(":FETCh:ARRay:VOLT? (@{})".format(self.channel))
            # self.handler.write(":OUTP2:RECall 1")  # 偏压供电
        except Exception as e:
            self.handler.write(":OUTPut{}:STATe 0".format(self.channel))
        else:
            y = y_result.strip().split(",")
            x = x_result.strip().split(',')
            y = [float(i) for i in y]
            x = [float(i) for i in x]
            # 关闭
            self.handler.write(":OUTPut{}:STATe 0".format(self.channel))
            return [x, y]

    def __query_opc(self):
        w = 0
        while w != "1":
            self.handler.ask("*opc?")
            w = self.handler.read().strip()
            time.sleep(1)
